/***************************************************************************************************
 *
 * Project: HZFlashcards
 *
 * Class: HZFlashcardsDataModel
 *
 * File name: HZFlashcardsDataModel.java
 *
 * Aug 23, 2007
 *
 **************************************************************************************************/

package com.eastmountain.chinese.db.flashcards.connection;

import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.eastmountain.util.logging.LoggerUtils;

/*******************************************************************************
 * 
 * CLASS HZFlashcardsDataModel
 * 
 ******************************************************************************/

public class DBProperties {

    DBAccessData dbAccessData = null;

    private JFrame frame = null;

    private static String  SOURCE_DB_PCRS        = "PCRS";
    private static String  SOURCE_DB_CHPODS      = "CHPODS";
    private static String  SOURCE_DB_MIT         = "MIT";
    private static String  SOURCE_DB_RLC         = "RLC";
    private static String  SOURCE_DB_TESTRLC     = "TESTRLC";
    
    private static String sourceDB;
    
    private static String  PCRS_DB_ACCESS_DATA    = PCRSDBAccessData.class
             .getName();
    private static String  CHPODS_DB_ACCESS_DATA  = CHPODSDBAccessData.class
             .getName();
    private static String  MIT_DB_ACCESS_DATA     = MITDBAccessData.class
             .getName();
    private static String  RLC_DB_ACCESS_DATA     = RLCDBAccessData.class
             .getName();
    private static String  TESTRLC_DB_ACCESS_DATA = TESTRLCDBAccessData.class
             .getName();

    private static String  projectName = "gcflashcards";
    protected static Logger logger     = LoggerUtils.setUpLogger( projectName );

    /***************************************************************************
     * 
     *  CLASS - DBProperties
     * 
     **************************************************************************/

    public DBProperties( String sourceDB ) {

        this.sourceDB = sourceDB;

        dbAccessData = setDBAccessData( sourceDB );

    }

    /***************************************************************************
     * 
     * setDBAccessData
     * 
     **************************************************************************/

    private DBAccessData setDBAccessData( String sourceDB ) {

        DBAccessData dbAccessData = null;

        try {

            if ( sourceDB.equals( SOURCE_DB_PCRS ) ) {

                System.out.println( "*****  Attempting to load "
                        + PCRS_DB_ACCESS_DATA );

                dbAccessData = (PCRSDBAccessData) Class.forName(
                        PCRS_DB_ACCESS_DATA ).newInstance();

            } else if ( sourceDB.equals( SOURCE_DB_CHPODS ) ) {

                System.out.println( "*****  Attempting to load "
                        + CHPODS_DB_ACCESS_DATA );

                dbAccessData = (CHPODSDBAccessData) Class.forName(
                        CHPODS_DB_ACCESS_DATA ).newInstance();

            } else if ( sourceDB.equals( SOURCE_DB_MIT ) ) {

                System.out.println( "*****  Attempting to load "
                        + MIT_DB_ACCESS_DATA );

                dbAccessData = (MITDBAccessData) Class.forName(
                        MIT_DB_ACCESS_DATA ).newInstance();

            } else if ( sourceDB.equals( SOURCE_DB_RLC ) ) {

                System.out.println( "*****  Attempting to load "
                        + RLC_DB_ACCESS_DATA );

                dbAccessData = (RLCDBAccessData) Class.forName(
                        RLC_DB_ACCESS_DATA ).newInstance();

            } else if ( sourceDB.equals( SOURCE_DB_TESTRLC ) ) {

                System.out.println( "*****  Attempting to load "
                        + TESTRLC_DB_ACCESS_DATA );

                dbAccessData = (TESTRLCDBAccessData) Class.forName(
                        TESTRLC_DB_ACCESS_DATA ).newInstance();

            } else {

                System.out.println( "*****  Attempting to load default "
                        + PCRS_DB_ACCESS_DATA );

                dbAccessData = (PCRSDBAccessData) Class.forName(
                        PCRS_DB_ACCESS_DATA ).newInstance();

            }

        } catch ( Exception exception ) {

            JOptionPane.showMessageDialog( frame, exception.getMessage(),
                    "DB Error", JOptionPane.ERROR_MESSAGE );

            exception.printStackTrace();

            System.exit( 12 );

        }

        System.out.println( "*****  Loaded " + dbAccessData.getClassName() );

        return( dbAccessData );

    }

    public DBAccessData getDbAccessData() {
        
        return( dbAccessData );
        
    }

}